package com.example.seckilldemo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.seckilldemo.mapper.OrderMapper;
import com.example.seckilldemo.mapper.SeckillOrderMapper;
import com.example.seckilldemo.pojo.Order;
import com.example.seckilldemo.pojo.SeckillGoods;
import com.example.seckilldemo.pojo.SeckillOrder;
import com.example.seckilldemo.pojo.User;
import com.example.seckilldemo.service.IOrderService;
import com.example.seckilldemo.service.ISeckillGoodsService;
import com.example.seckilldemo.service.ISeckillOrderService;
import com.example.seckilldemo.utils.MD5Utils;
import com.example.seckilldemo.utils.UUIDUtil;
import com.example.seckilldemo.vo.GoodsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lambyang
 * @since 2024-03-19
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private ISeckillGoodsService seckillGoodsService;
    @Autowired
    private SeckillOrderMapper seckillOrderMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Transactional
    @Override
    public Order seckill(User user, GoodsVo goodsVo) {
        ValueOperations valueOperations=redisTemplate.opsForValue();
        //秒杀库存扣减
        SeckillGoods seckillGoods = seckillGoodsService.getOne(new QueryWrapper<SeckillGoods>().eq("goods_id", goodsVo.getId()));
        seckillGoods.setStockCount(seckillGoods.getStockCount() - 1);
        // seckillGoodsService.updateById(seckillGoods);
        // 修改时判断库存是否大于0
        seckillGoodsService.update(new UpdateWrapper<SeckillGoods>()
               .setSql("stock_count = stock_count-1").eq("goods_id",goodsVo.getId()).gt("stock_count",0));
       if (seckillGoods.getStockCount()<1){
           //判断是否有库存
           valueOperations.set("isStockEmpty:"+goodsVo.getId(),"0");
       }

        //生成订单
        Order order = new Order(user.getId(), goodsVo.getId(), 0L, goodsVo.getGoodsName(), 1, seckillGoods.getSeckillPrice(), 1, 0, new Date());
        orderMapper.insert(order);

        //生成秒杀订单
        SeckillOrder seckillOrder = new SeckillOrder(user.getId(), order.getId(), goodsVo.getId());
        //存入redis
        redisTemplate.opsForValue().set("order:"+user.getId()+";"+goodsVo.getId(),seckillOrder);
        seckillOrderMapper.insert(seckillOrder);
        return order;
    }

    /**
     * 生成path
     * @param goodsId
     * @param user
     * @return
     */
    @Override
    public String createPath(Long goodsId,User user) {
        String str= MD5Utils.md5(UUIDUtil.uuid()+"123456");
        redisTemplate.opsForValue().set("seckillPath:"+user.getId()+":"+goodsId,str,60, TimeUnit.SECONDS);
        return str;
    }

    /**
     * 校验path
     * @param goodsId
     * @param user
     * @return
     */
    @Override
    public boolean checkPath(Long goodsId, User user,String path) {
        if (StringUtils.isEmpty(path)|| Objects.isNull(user)||goodsId==null){
            return false;
        }
        String redisPath= (String) redisTemplate.opsForValue().get("seckillPath:"+user.getId()+":"+goodsId);
        return path.equals(redisPath);
    }

    /**
     * 校验验证码
     * @param goodsId
     * @param captcha
     * @param user
     * @return
     */
    @Override
    public boolean checkCaptcha(Long goodsId, String captcha, User user) {
        if (StringUtils.isEmpty(captcha)||goodsId==null){
            return false;
        }
        String redisCaptcha= (String) redisTemplate.opsForValue().get("captcha:"+user.getId()+":"+goodsId);
        return captcha.equals(redisCaptcha);
    }
}
